<?php 	
namespace Home\Controller;
use Think\Controller;
use Think\Template\Driver\Mobile;
use Model\usersModel;
use Admin\Controller\UsersController;

class SpecialController extends Controller {
    function __destruct(){                                                                         // 析构方法  写入用户访问和操作日志
       // require '/home/wwwroot/default/world/behavior.php';
    	//require 'behavior.php';
    }
  public function waitlist(){
	  $special=M('special');
		$projectcategory = M ( 'projectcategory' );
		$typeId = $_REQUEST ['id'];
	   if (I('post.name')!=null) {
      $map['sname']  = array('like',  '%'.I('post.name').'%');
      $map['majorteacher']  = array('like',  '%'.I('post.name').'%');
      $map['_logic'] = 'or';
      $where['_complex'] = $map;
      $where['statue']  = array('eq',0);
	   	$count=$special->where($where)->count();
		  $p=getpage($count,8);
		  $show=$special->where($where )->limit($p->firstRow,$p->listRows)->order('sid ')->select();
		
		}
     else{
        	if (strlen ( $typeId ) != 1 || $projectcategory->where ( "id='$typeId'" )->select () == null) {
			$typeId = 'all';
		}
		
		if ($typeId == 'all' || $typeId == null) {
			$count=$special->count();
			$p = getpage($count,8);
			$show=$special->where(" statue =0" )->order ( 'sid' )->limit($p->firstRow, $p->listRows)->select ();
		} 
		else {
			$count = $special->where("statue =0 AND pcid='$typeId'" )->count (); // 有多少审核通过的项目
			 $p = getpage($count,8);
			$show = $special->where("statue =0 AND pcid='$typeId'" )->order ( 'sid' )->limit($p->firstRow, $p->listRows)->select ();
		}
        }
		
	
		for($i = 0; $i < count($show); $i ++) { // 有多少审核通过的项目就循环多少次
			$sdate = $show [$i] ['sdate']; // 取出发布时间
			$sub = substr ( $sdate, 11, 2 ); // 将发布时间的时截取出来
			if ($sub < 12) {
				$show [$i] ['time'] = "上午"; // 判断是否小于12，如果小于12，就是上午，否则就是下午
			    
			} else {
				$show [$i] ['time'] = "下午";
			}
		}


		
		$this->assign ( 'show', $show );
		$this->assign('page', $p->show()); // 赋值分页输出
	    $this->display();

}

public function detail(){
	  $num=1;
	  $sid['sid']=$_GET['id'];
    $special=M('special');
    $specialcourse=M('specialcourse');
    $infer=$specialcourse->select();
    //var_dump($infer);
    $data=array();
    for ($i=0; $i <count($infer); $i++) { 
       $time=time();
       $startdate=$infer[$i]['startdate'];
       $enddate=$infer[$i]['enddate'];
      $data['scid']=$infer[$i]['scid'];

    if ($time<strtotime($startdate)) {
      $statue[$i]['statue']=0;//未开始
    }
    elseif ($time>strtotime($startdate) && $time<strtotime($enddate)) {
      $statue[$i]['statue']=1;//开始中
    }
     elseif ($time>strtotime($enddate)) {
      $statue[$i]['statue']=2;//结束
    }
    $data['statue']= $statue[$i]['statue'];
     $res= $specialcourse->save($data);
    $data=null;
    }
    $number=1;
    $info=$special->field('sid,sname,majorteacher,sdate')->where($sid)->select();
    $cinfo=$specialcourse->field('scid,scname,content,startdate,totalticket,restticket,enddate,statue')->where($sid)->select();
    for ($i=0; $i <count($cinfo) ; $i++) { 
    	$content=$specialcourse->getField('content');
    	$str=strip_tags($content);
    	$str=str_replace("","",$str);
    	$re=mb_substr($str,0,30,'utf-8');
    	$cinfo[$i]['content']=$re;
    }
    $this->assign(array('num'=>$num,'info'=>$info,'cinfo'=>$cinfo,'number'=>$number));
	$this->display();
}

      public function cdetail()
    {


      $where=$_GET['id'];
    /*  $restticket=M('specialcourse')->where("scid='$where'")->getField('restticket');
      if ($restticket===0) {
       $data['']
      }*/
      $cinfo=M('')->table(array('specialcourse'=>'c','special'=>'s'))->where("c.sid=s.sid AND c.scid='$where'")->find();
      // dump($cinfo);die;
      $this->assign('cinfo',$cinfo);
    	$this->display();
    }
       public function endlist()
    {

   $special=M('special');
    $projectcategory = M ( 'projectcategory' );
    $typeId = $_REQUEST ['id'];
    
    $name=I('post.name');
     if (I('post.name')!=null) {
       $where['sname']  = array('like',  '%'.I('post.name').'%');
       $where['majorteacher']  = array('like',  '%'.I('post.name').'%');
       $where['_logic']='or';
       $map['_complex']=$where;
       $map['statue']='1';
      
    $count=$special->where($map)->order("sid")->count();
    $p=getpage($count,8);
    $show=$special->where($map )->limit($p->firstRow,$p->listRows)->order('sid desc')->select();
    
    }
     else{
          if (strlen ( $typeId ) != 1 || $projectcategory->where ( "id='$typeId'" )->select () == null) {
      $typeId = 'all';
    }
    
    if ($typeId == 'all' || $typeId == null) {
      $count=$special->count();
      $p = getpage($count,8);
      $show=$special->where(" statue =1" )->order ( 'sid' )->limit($p->firstRow, $p->listRows)->select ();
    } 
    else {
      $count = $special->where("statue =1 AND pcid='$typeId'" )->count (); // 有多少审核通过的项目
       $p = getpage($count,8);
      $show = $special->where("statue =1 AND pcid='$typeId'" )->order ( 'sid' )->limit($p->firstRow, $p->listRows)->select ();
    }
        }
    
  
    for($i = 0; $i < count($show); $i ++) { // 有多少审核通过的项目就循环多少次
      $sdate = $show [$i] ['sdate']; // 取出发布时间
      $sub = substr ( $sdate, 11, 2 ); // 将发布时间的时截取出来
      if ($sub < 12) {
        $show [$i] ['time'] = "上午"; // 判断是否小于12，如果小于12，就是上午，否则就是下午
          
      } else {
        $show [$i] ['time'] = "下午";
      }
    }


    
    $this->assign ( 'show', $show );
    $this->assign('page', $p->show()); // 赋值分页输出
      $this->display();
    }
      public function myjoin()
    {
      $Hint = new \Operation\Controller\HintController (); // 实例化Hint
      $Hint->checkLogin ( 1 ); 
      $number=1;
      $specialcourse=M('specialcourse');
      $stu_course=M('stu_course');
      $uid=session('userUid');
      $scinfo=M('')->table(array('stu_course'=>'stu','specialcourse'=>'spec'))->where("stu.uid='$uid' AND spec.scid=stu.scid")->select();
      $this->assign(array('number'=>$number,'scinfo'=>$scinfo));
    	$this->display();
    }

    //报名课程
    function cjoin()
    {
       $Hint = new \Operation\Controller\HintController (); // 实例化Hint
     $Hint->checkLogin ( 1 ); 
      $data=array();
      $data['uid']=session ( 'userUid' );
      $data['scid']=$_GET['scid'];
      $stu_course=M('stu_course');
      $specialcourse=M('specialcourse');
      $isJ=$stu_course->where($data)->select();
      if ($isJ) {
        echo "YQ";
      }
      else{
         $d['scid']=$_GET['scid'];
         $specialcourse->where($d)->setDec('restticket');
         $restticket=$specialcourse->where($d)->getField('restticket');
         $re=$stu_course->add($data);
      if ($re) {
        echo $restticket; 
      }   
      else{
       echo "false";
      }
      }

      
    }



    function stu_course_Del()
    {
      $Hint = new \Operation\Controller\HintController (); // 实例化Hint
      $Hint->checkLogin ( 1 ); 
      $ssid['ssid']=$_GET['id'];
      $scid['scid']=M('stu_course')->where($ssid)->getField('scid');
      $specialcourse=M('specialcourse');
      $re=M('stu_course')->where($ssid)->delete();
      if ($re) {
        $specialcourse->where($scid)->setInc('restticket');
        $Hint->mysuccee("课程取消成功",'2');
        
      }
      else{
        $Hint->myerror("课程取消失败");
      }

    }
function estimate()
{
   $stu_course=M('stu_course');
   $data['scid']=$_GET['scid'];
   $data['uid']=session ( 'userUid' );
   $state=$stu_course->where($data)->find();
   if($state) 
     {
      echo "false"; //已经报名
     }
     else{
      echo "true";//没有报名
     }
   
}
    function cancelCourse()
    { 
       $Hint = new \Operation\Controller\HintController (); // 实例化Hint
      $Hint->checkLogin ( 1 ); 
       $specialcourse=M('specialcourse');
       $stu_course=M('stu_course');
       $data=array();
       $data['uid']=session ( 'userUid' );
       $data['scid']=$_GET['scid'];
       $re=$stu_course->where($data)->delete(); 
       $cdata['scid']=$_GET['scid'];
       $restticket=$specialcourse->where($cdata)->getField('restticket');
       $newrest['restticket']=$restticket+1;
       $specialcourse->where($cdata)->save($newrest);
       $newrestticket=$specialcourse->where($cdata)->getField('restticket');
       if ($newrestticket) {
         echo $newrestticket;
       }
       else{
        echo "false";
       }       
    }

    public function download(){
    $scid=$_GET['scid'];
    $res=D('specialcourse')->find($scid);
    $url='D:/xampp/htdocs'.__ROOT__.'/Public/UPLOADS/specialcourse/'.$res['attach'];
    $this->download_file($url,$res['attach']);
  }
 function download_file($file,$showname){
    if(is_file($file)){
        $length = filesize($file);
        $type = mime_content_type($file);
        // $showname =  ltrim(strrchr($file,'/'),'/');
        header("Content-Description: File Transfer");
        header('Content-type: ' . $type);
        header('Content-Length:' . $length);
         if (preg_match('/MSIE/', $_SERVER['HTTP_USER_AGENT'])) { //for IE
             header('Content-Disposition: attachment; filename="' . rawurlencode($showname) . '"');
         } else {
             header('Content-Disposition: attachment; filename="' . $showname . '"');
         }
         ob_clean(); 
         flush(); 
         readfile($file);
         exit;
     } else {
         exit('文件已被删除！');
     }
 }

public function set_session($scid){
       session('courseLogin',$scid);
}


}











 ?>